package com.crazymakercircle.node;

import com.crazymakercircle.constants.ServerConstants;
import com.crazymakercircle.entity.TaskData;
import com.crazymakercircle.util.JsonUtil;
import com.crazymakercircle.zk.CuratorZKclient;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

/**
 * IM 节点的ZK协调客户端
 * create by 尼恩 @ 疯狂创客圈
 **/
@Data
@Slf4j
public class TaskNode
{

    //Zk curator 客户端
    private CuratorFramework client = null;

    //保存当前Znode节点的路径，创建后返回
    private String pathRegistered = null;

    private TaskData taskData = null;

    private static TaskNode singleInstance = null;
    private boolean inited = false;

    //取得单例
    public synchronized static TaskNode getInst()
    {

        if (null == singleInstance)
        {

            singleInstance = new TaskNode();
            singleInstance.taskData = new TaskData();
        }
        return singleInstance;
    }

    private TaskNode()
    {

    }

    // 在zookeeper中创建临时节点
    public synchronized void createNode()
    {

        if (inited)
        {
            return;
        }
        inited = true;
        if (null == client)
        {
            this.client = CuratorZKclient.instance.getClient();
        }
        if (null == taskData)
        {
            taskData = new TaskData();
        }

        createParentIfNeeded(ServerConstants.TASK_PATH);

        // 创建一个 ZNode 节点
        // 节点的 payload 为当前worker 实例

        try
        {
            byte[] payload = JsonUtil.object2JsonBytes(taskData);

            pathRegistered = client.create()
                    .creatingParentsIfNeeded()
                    .withMode(CreateMode.PERSISTENT_SEQUENTIAL)
                    .forPath(ServerConstants.PATH_WORKER_PREFIX, payload);

            //为node 设置id
            taskData.setRunId(getId());
            log.info("本地节点, path={}, id={}", pathRegistered, taskData.getRunId());
        } catch (Exception e)
        {
            e.printStackTrace();
        }

    }


    /**
     * 取得IM 节点编号
     *
     * @return 编号
     */
    public String getId()
    {

        return TaskData.getRunIdByPath(pathRegistered);

    }

    /**
     * 取得IM 节点编号
     *
     * @param path 路径
     * @return 编号
     */


    /**
     * 创建父节点
     *
     * @param managePath 父节点路径
     */
    private void createParentIfNeeded(String managePath)
    {

        try
        {
            Stat stat = client.checkExists().forPath(managePath);
            if (null == stat)
            {
                client.create()
                        .creatingParentsIfNeeded()
                        .withProtection()
                        .withMode(CreateMode.PERSISTENT)
                        .forPath(managePath);
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }

    }


    /**
     * 返回本地的节点信息
     *
     * @return 本地的节点信息
     */
    public TaskData getLocalNodeInfo()
    {
        return taskData;
    }

}